<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="企业名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="企业名称"></el-input>
    </el-form-item>
    <el-form-item label="企业地址" prop="addres">
      <el-input v-model="dataForm.addres" placeholder="企业地址"></el-input>
    </el-form-item>
    <el-form-item label="企业法人" prop="operName">
      <el-input v-model="dataForm.operName" placeholder="企业法人"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="telephone">
      <el-input v-model="dataForm.telephone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="其他联系" prop="otherConnect">
      <el-input v-model="dataForm.otherConnect" placeholder="其他联系"></el-input>
    </el-form-item>
    <el-form-item label="企业简介" prop="introduction">
      <el-input v-model="dataForm.introduction" placeholder="企业简介"></el-input>
    </el-form-item>
    <el-form-item label="上年度产出" prop="frontYearProduce">
      <el-input v-model="dataForm.frontYearProduce" placeholder="上年度产出"></el-input>
    </el-form-item>
    <el-form-item label="企业员工数" prop="totalStaff">
      <el-input v-model="dataForm.totalStaff" placeholder="企业员工数"></el-input>
    </el-form-item>
    <el-form-item label="所属企业类型" prop="enterpriseType">
      <el-input v-model="dataForm.enterpriseType" placeholder="所属企业类型"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          addres: '',
          operName: '',
          telephone: '',
          otherConnect: '',
          introduction: '',
          frontYearProduce: '',
          totalStaff: '',
          enterpriseType: ''
        },
        dataRule: {
          name: [
            { required: true, message: '企业名称不能为空', trigger: 'blur' }
          ],
          addres: [
            { required: true, message: '企业地址不能为空', trigger: 'blur' }
          ],
          operName: [
            { required: true, message: '企业法人不能为空', trigger: 'blur' }
          ],
          telephone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          otherConnect: [
            { required: true, message: '其他联系不能为空', trigger: 'blur' }
          ],
          introduction: [
            { required: true, message: '企业简介不能为空', trigger: 'blur' }
          ],
          frontYearProduce: [
            { required: true, message: '上年度产出不能为空', trigger: 'blur' }
          ],
          totalStaff: [
            { required: true, message: '企业员工数不能为空', trigger: 'blur' }
          ],
          enterpriseType: [
            { required: true, message: '所属企业类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/enterprisemain/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.enterpriseMain.name
                this.dataForm.addres = data.enterpriseMain.addres
                this.dataForm.operName = data.enterpriseMain.operName
                this.dataForm.telephone = data.enterpriseMain.telephone
                this.dataForm.otherConnect = data.enterpriseMain.otherConnect
                this.dataForm.introduction = data.enterpriseMain.introduction
                this.dataForm.frontYearProduce = data.enterpriseMain.frontYearProduce
                this.dataForm.totalStaff = data.enterpriseMain.totalStaff
                this.dataForm.enterpriseType = data.enterpriseMain.enterpriseType
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/enterprisemain/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'addres': this.dataForm.addres,
                'operName': this.dataForm.operName,
                'telephone': this.dataForm.telephone,
                'otherConnect': this.dataForm.otherConnect,
                'introduction': this.dataForm.introduction,
                'frontYearProduce': this.dataForm.frontYearProduce,
                'totalStaff': this.dataForm.totalStaff,
                'enterpriseType': this.dataForm.enterpriseType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
